package edu.guigu.appquickstart.simpleview.subitem;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.snackbar.Snackbar;

import edu.guigu.appquickstart.R;

public class DialogboxActivity extends AppCompatActivity {

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialogbox);

        // Actionbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // getSupportActionBar().setTitle("对话框");  // 默认使用的清单中 <action中的 android:label="对话框"
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Snackbar
        snackbarBox();
        // 确定对话框
        confirmBox();
        // 进度对话框
        progressDialogBox();
        // 列表对话框
        listDialogBox();
        // 底部对话框
        bottomSheetDialogBox();
    }

    private void bottomSheetDialogBox() {
        findViewById(R.id.btn_dialog_bottomsheet).setOnClickListener(v -> {
            BottomSheetDialog bsDialog = new BottomSheetDialog(DialogboxActivity.this);
            // 加载dialog的布局文件,设置到dialog上
            final LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
            View bottomView = layoutInflater.inflate(R.layout.layout_bottomsheet, null);
            bsDialog.setContentView(bottomView);

            bsDialog.setCancelable(false);
            bsDialog.show();

            // 拿到dialog上视图上的button，并做监听处理
            bottomView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bsDialog.dismiss();
                }
            });
            bottomView.findViewById(R.id.save_photo).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(DialogboxActivity.this, "hit~~~", Toast.LENGTH_SHORT).show();
                }
            });
        });
    }

    private void listDialogBox() {
        findViewById(R.id.btn_dialog_list).setOnClickListener(v -> {
            final String[] lessons = {"Java", "C++", "C#", "Python", "JavaScript"};
            final boolean[] ischoices = new boolean[5];
            AlertDialog listDialog = new AlertDialog.Builder(DialogboxActivity.this)
                    .setIcon(R.mipmap.ic_launcher)
                    .setTitle("请选择您的课程")
                    //.setSingleChoiceItems(lessons, 0, null)
                    //.setMultiChoiceItems(lessons, ischoices, null)
                    .setItems(lessons, (dialog, which) -> {
                        // getApplicationContext()
                        Toast.makeText(getApplicationContext(), "你选择了" + lessons[which], Toast.LENGTH_SHORT).show();
                    })
                    .create();
            listDialog.show();
        });
    }

    private void progressDialogBox() {
        findViewById(R.id.btn_dialog_progress).setOnClickListener(v -> {
            // ProgressDialog.show(DialogboxActivity.this, "loading","资源加载中...", false, true);
            ProgressDialog progressDialog = new ProgressDialog(DialogboxActivity.this);
            progressDialog.setTitle("加载中");
            progressDialog.setMessage("资源加载中...请稍后");
            progressDialog.setCancelable(true);
            // STYLE_HORIZONTAL 水平
            // STYLE_SPINNER  圆形
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();
        });
    }

    private void confirmBox() {
        findViewById(R.id.btn_dialog_confirm).setOnClickListener(V -> {
            AlertDialog alertDialog = new AlertDialog.Builder(DialogboxActivity.this)
                    .setTitle("警告")
                    .setMessage("确认退出吗？")
                    .setPositiveButton("确认", (dialog, which) -> {
                        Toast.makeText(this, "确认", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", null)
                    .setCancelable(false)
                    .create();
            alertDialog.show();
        });
    }

    private void snackbarBox() {
        findViewById(R.id.btn_dialog_snackbar).setOnClickListener(v -> {

            View rootView = findViewById(R.id.main); // 这是当前页面的根视图
            Snackbar.make(rootView, "这是一个Snackbar提示", Snackbar.LENGTH_LONG)
                    .setAction("撤销", vv -> {
                        Toast.makeText(this, "你点撤销了！", Toast.LENGTH_SHORT).show();
                    })
                    .show();
        });
    }
}